package dao;

import com.sun.org.apache.xpath.internal.operations.Bool;
import pojo.MsgInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class jdbc extends JDBCBaseDao {
    MsgInfo msg = new MsgInfo();

    /**
     * 注册
     *
     * @param account
     * @param password
     * @return
     */
    public Boolean registers(String account, String password) {
        if (this.seluser(account)) {
            return false;
        }
        if (account!=""&&password!="") {
            String sql = "INSERT INTO `user`(`account`, `password`) VALUES (?, ?);";
            List<Object> ls = new ArrayList<>();
            ls.add(account);
            ls.add(password);
            if (super.iudsql(sql, ls)) {
                msg.setStatus(1);
                msg.setMsg("注册成功");
                return true;
            }
        }
        return false;
    }

    /**
     * 查询用户名
     *
     * @param account
     * @return
     */
    public Boolean seluser(String account) {
        Connection conn = super.getConnection();
        PreparedStatement stmt = null;
        try {
            String sql = "SELECT * FROM `user` WHERE account = ?;";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, account);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return true;
            }
        } catch (SQLException e) {
            System.out.println("查询失败");
        }
        return false;
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     * @return
     */
    public Boolean login(String account, String password) {
        Connection conn = super.getConnection();
        PreparedStatement stmt = null;
        try {
            if (account != "" && password != "") {
                String sql = "SELECT * FROM `user` WHERE account = ? AND password = ?;";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, account);
                stmt.setString(2, password);
                ResultSet rs = stmt.executeQuery();
                if (rs.next()) {
                    return true;
                }
                conn.close();
            }
        } catch (SQLException e) {
            System.out.println("登录失败");
        }
        return false;
    }
}
